<?php
/*                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                            O\ = /O
 *                        ____/`---'\____
 *                      .   ' \\| |// `.
 *                       / \\||| : |||// \
 *                     / _||||| -:- |||||- \
 *                       | | \\\ - /// | |
 *                     | \_| ''\---/'' | |
 *                      \ .-\__ `-` ___/-. /
 *                   ___`. .' /--.--\ `. . __
 *                ."" '< `.___\_<|>_/___.' >'"".
 *               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *                 \ \ `-. \_ __\ /__ _/ .-` / /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *
 *         .............................................
 *                  佛祖保佑             永无BUG
 *
 * ======================================================
 * @author: Ethan Lu <ethan.lu@qq.com>
 * 
 */

namespace frontend\components\sms;

use yii\base\InvalidConfigException;
use yii\validators\Validator;

class SmsValidator extends Validator
{

    public $code = null;

    public $targetField = 'mobile';

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if($this->message === null) {
            $this->message = '短信验证码不正确';
        }
    }

    public function validateAttribute($model, $attribute)
    {
        if($model->$attribute != \Yii::$app->session->get('code_'.$model->{$this->targetField})) {
            $this->addError($model, $attribute, $this->message);
        }
    }


}